﻿using AutoMapper;
using DDD.Application;
using GoodsModule.Infrastructure.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GoodsModule.Application.Commands
{
    public class DeleteGoodsByGoodsCategoryIdCommannd : ICommand
    {
        public DeleteGoodsByGoodsCategoryIdCommannd(int goodsCategoryId)
        {
            GoodsCategoryId = goodsCategoryId;
        }

        public int GoodsCategoryId { get; set; }
    }

    public class DeleteGoodsByGoodsCategoryIdCommanndHandler(IMapper mapper, GoodsRepository repository, ILogger<DeleteGoodsByGoodsCategoryIdCommanndHandler> logger)
        : ICommandHandler<DeleteGoodsByGoodsCategoryIdCommannd>
    {
        public async Task Handle(DeleteGoodsByGoodsCategoryIdCommannd request, CancellationToken cancellationToken)
        {
            var list = await repository.GetAll().Where(e => e.GoodsCategoryId == request.GoodsCategoryId).ToListAsync();
            foreach (var item in list)
            {
                item.Delete();
                await repository.UpdateAsync(item);
            }
        }
    }
}
